#!/bin/bash

# SPAM3UK 服务器部署脚本
# 用于部署包含HTTP/2修复的最新代码

echo "🚀 SPAM3UK 服务器部署脚本"
echo "========================================"
echo "部署标签: SPAM3UK-FIXBUG-C8"
echo "修复内容: HTTP/2连接问题 + 服务器绑定配置"
echo "开始时间: $(date)"
echo ""

# 1. 停止当前服务
echo "1️⃣ 停止当前服务..."
echo "----------------------------------------"
sudo pkill -f spam3uk 2>/dev/null || echo "  没有运行中的spam3uk进程"
sudo pkill -f "go run main.go" 2>/dev/null || echo "  没有运行中的go run进程"
sleep 3

# 2. 备份当前版本
echo ""
echo "2️⃣ 备份当前版本..."
echo "----------------------------------------"
if [ -f "spam3uk" ]; then
    cp spam3uk spam3uk.backup.$(date +%Y%m%d_%H%M%S)
    echo "  ✅ 已备份当前可执行文件"
else
    echo "  ℹ️ 没有找到现有可执行文件"
fi

# 3. 拉取最新代码
echo ""
echo "3️⃣ 拉取最新代码..."
echo "----------------------------------------"
git clone https://gitee.com/andershsueh/spam3uk.git
cd spam3uk
git fetch origin
git checkout master
git pull origin master
echo "  ✅ 代码更新完成"
cd ..
mv ./spam3uk/spam3uk-config.json ./spam3uk/spam3uk-config.b1
cp ./spam3uk-config.json ./spam3uk/
echo "  ✅ CONFIG COPY INTO SPAM3, 更新完成"


# 4. 检查关键修复
echo ""
echo "4️⃣ 验证关键修复..."
echo "----------------------------------------"
cd spam3uk
if grep -q "ForceAttemptHTTP2.*false" internal/scraper/vehicle_scraper.go; then
    echo "  ✅ HTTP/2禁用配置存在"
else
    echo "  ❌ HTTP/2禁用配置缺失"
fi

if grep -q "Proto.*HTTP/1.1" internal/scraper/vehicle_scraper.go; then
    echo "  ✅ HTTP/1.1强制配置存在"
else
    echo "  ❌ HTTP/1.1强制配置缺失"
fi

if grep -q '"host": "0.0.0.0"' spam3uk-config.json; then
    echo "  ✅ 服务器绑定配置正确"
else
    echo "  ❌ 服务器绑定配置错误"
fi

# 5. 编译新版本
echo ""
echo "5️⃣ 编译新版本..."
echo "----------------------------------------"
go mod tidy
go build -o spam3uk main.go
if [ $? -eq 0 ]; then
    echo "  ✅ 编译成功"
    ls -la spam3uk
else
    echo "  ❌ 编译失败"
    exit 1
fi

# 6. 配置权限
echo ""
echo "6️⃣ 配置权限..."
echo "----------------------------------------"
chmod +x spam3uk
echo "  ✅ 可执行权限设置完成"

# 7. 启动服务
echo ""
echo "7️⃣ 启动服务..."
echo "----------------------------------------"
nohup ./spam3uk > server.log 2>&1 &
SPAM3UK_PID=$!
echo "  ✅ 服务已启动，PID: $SPAM3UK_PID"
sleep 5

# 8. 健康检查
echo ""
echo "8️⃣ 健康检查..."
echo "----------------------------------------"
for i in {1..10}; do
    if curl -s http://localhost:5700/health > /dev/null; then
        echo "  ✅ 服务启动成功"
        curl http://localhost:5700/health
        break
    else
        echo "  ⏳ 等待服务启动... ($i/10)"
        sleep 2
    fi
    
    if [ $i -eq 10 ]; then
        echo "  ❌ 服务启动失败"
        echo "  📋 查看错误日志:"
        tail -20 server.log
        exit 1
    fi
done

# 9. 测试关键功能
echo ""
echo "9️⃣ 测试抓取功能..."
echo "----------------------------------------"
echo "  🧪 测试HTTP/1.1修复是否生效..."
curl -X POST http://localhost:5700/api/scrape_all
if [ $? -eq 0 ]; then
    echo "  ✅ 抓取API调用成功"
else
    echo "  ❌ 抓取API调用失败"
fi

# 10. 部署完成
echo ""
echo "✨ 部署完成!"
echo "========================================"
echo "服务地址: http://$(hostname -I | awk '{print $1}'):5700"
echo "健康检查: curl http://localhost:5700/health"
echo "抓取测试: curl -X POST http://localhost:5700/api/scrape_all"
echo "查看日志: tail -f server.log"
echo "完成时间: $(date)"
echo ""
echo "🎯 下一步:"
echo "1. 验证外部访问: http://101.201.32.200:5700/web/"
echo "2. 测试抓取功能: curl -X POST http://101.201.32.200:5700/api/scrape_all"
echo "3. 监控服务日志: tail -f server.log"
